using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using XNAProjectLibrary;
using XNAProject.Core;
using XNAProject.GameElements;

namespace XNAProject
{
    public class XNAGame : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;                              //graphics manager
        public SpriteBatch spriteBatch;                                     

        private InputHandler input;                                         //input manager
        private GameStateManager gameManager;                               //gamestatemanager

        public ITitleIntroState TitleIntroState;                            //game menu states
        public IStartMenuState StartMenuState;
        public IHostMenuState HostMenuState;
        public IJoinMenuState JoinMenuState;        
        public IOptionsMenuState OptionsMenuState;
        public IPrepareState PrepareState;
        public IFleetManagerState FleetManagerState;
        public IPlayState PlayState;
        public IResultState ResultState;
        public IFloatMenuState FloatMenuState;
        public ICreditsState CreditsState;

        public SoundManager Sound;                                          //sound manager
        
        private Player player;

        public XNAGame()
        {
            graphics = new GraphicsDeviceManager(this);                     //set up graphics device
            Services.AddService(typeof(GraphicsDeviceManager), graphics);
            Content.RootDirectory = "Content";                              //specify contents directory

            //graphics.PreferMultiSampling = true;                            //graphics settings

            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 1024;

            //graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);

            Sound = new SoundManager(this, "XNAGameSounds");                //initialize sound manager

            input = new InputHandler(this);                                 //initialize input hangler
            Components.Add(input);            

            player = new Player(this);
            Components.Add(player);
            Services.AddService(typeof(Player), player);

            gameManager = new GameStateManager(this);                       //initialize game manager
            Components.Add(gameManager);                                    //add game manager to gamecomponents

            TitleIntroState = new TitleIntroState(this);                    //initialize gamestates
            StartMenuState = new StartMenuState(this);
            CreditsState = new CreditsState(this);
            JoinMenuState = new JoinMenuState(this);
            HostMenuState = new HostMenuState(this);
            OptionsMenuState = new OptionsMenuState(this);
            ResultState = new ResultState(this);
            FloatMenuState = new FloatMenuState(this);

            gameManager.ChangeState(TitleIntroState.Value);                //change gamestate to title intro state           
            //gameManager.ChangeState(PlayState.Value); 

            //this.IsMouseVisible = false;
            //Components.Add(new XNAProjectPointer(Game));
        }

        void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
            e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
            e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = displayMode.Width;
            e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;

        }

        protected override void Initialize()                                
        {
            base.Initialize();                                              
        }
        
        protected override void LoadContent()                               //initialize a new spritebatch
        {            
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
        }

        protected override void Update(GameTime gameTime)                   
        {
            Sound.Update(gameTime);
                                                                           //update sound

            base.Update(gameTime);                                          
        }

        protected override void Draw(GameTime gameTime)                     
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);                                            
        }
    }
}
